

def jwt_response_payload_handler(token, user, request):
    """系统的方法,只返回了token,不满足需求,所以改写,使用我们自己的"""

    return {
        "token":token,
        "username":user.username if user else "",
        "user_id":user.id if user else "",
    }

#使用手机号,用户名,邮箱登陆
from django.contrib.auth.backends import ModelBackend
import re
from users.models import User
import logging
logger =  logging.getLogger("django")
class UsernameMobileAuthBackend(ModelBackend):

    def authenticate(self, request, username=None, password=None, **kwargs):

        #1,判断username,是否是手机号,如果是,使用手机号查询用户
        user = None
        try:
            if re.match(r"1[3-9]\d{9}",username):
                user = User.objects.get(mobile=username)

            #2,通过用户名获取用户
            if not user:
                user = User.objects.get(username=username)
        except Exception as e:
            logger.error(e)
            user = None

        #3,判断用户是否存在,并且密码是否正确
        if user and user.check_password(password):
            return user

